namespace WeatherSystem.API.Services
{
    /// <summary>
    /// 错误恢复服务接口
    /// </summary>
    public interface IErrorRecoveryService
    {
        /// <summary>
        /// 执行带重试机制的操作
        /// </summary>
        Task<T> ExecuteWithRetryAsync<T>(
            Func<Task<T>> operation,
            int maxRetries = 3,
            TimeSpan? delay = null,
            Func<Exception, bool>? shouldRetry = null);

        /// <summary>
        /// 执行带重试机制的操作（无返回值）
        /// </summary>
        Task ExecuteWithRetryAsync(
            Func<Task> operation,
            int maxRetries = 3,
            TimeSpan? delay = null,
            Func<Exception, bool>? shouldRetry = null);

        /// <summary>
        /// 执行带降级机制的操作
        /// </summary>
        Task<T> ExecuteWithFallbackAsync<T>(
            Func<Task<T>> primaryOperation,
            Func<Task<T>> fallbackOperation,
            Func<Exception, bool>? shouldFallback = null);

        /// <summary>
        /// 执行带超时控制的操作
        /// </summary>
        Task<T> ExecuteWithTimeoutAsync<T>(
            Func<CancellationToken, Task<T>> operation,
            TimeSpan timeout);

        /// <summary>
        /// 执行带断路器模式的操作
        /// </summary>
        Task<T> ExecuteWithCircuitBreakerAsync<T>(
            Func<Task<T>> operation,
            string circuitBreakerKey);

        /// <summary>
        /// 记录和追踪错误
        /// </summary>
        Task LogErrorAsync(Exception exception, string context, Dictionary<string, object>? additionalData = null);

        /// <summary>
        /// 获取系统健康状态
        /// </summary>
        Task<SystemHealthStatus> GetSystemHealthAsync();

        /// <summary>
        /// 触发错误恢复流程
        /// </summary>
        Task TriggerRecoveryAsync(string component, Exception exception);
    }

    /// <summary>
    /// 系统健康状态
    /// </summary>
    public class SystemHealthStatus
    {
        public string Status { get; set; } = "Healthy"; // Healthy, Degraded, Unhealthy
        public Dictionary<string, ComponentHealth> Components { get; set; } = new();
        public DateTime LastUpdated { get; set; } = DateTime.UtcNow;
        public List<string> Issues { get; set; } = new();
    }

    /// <summary>
    /// 组件健康状态
    /// </summary>
    public class ComponentHealth
    {
        public string Name { get; set; } = "";
        public string Status { get; set; } = "Healthy";
        public string? Description { get; set; }
        public DateTime LastCheck { get; set; } = DateTime.UtcNow;
        public long ResponseTime { get; set; } // 毫秒
        public Dictionary<string, object> Metadata { get; set; } = new();
    }

    /// <summary>
    /// 断路器状态
    /// </summary>
    public enum CircuitBreakerState
    {
        Closed,  // 正常状态
        Open,    // 断路状态
        HalfOpen // 半开状态
    }

    /// <summary>
    /// 断路器配置
    /// </summary>
    public class CircuitBreakerOptions
    {
        public int FailureThreshold { get; set; } = 5;
        public TimeSpan OpenTimeout { get; set; } = TimeSpan.FromMinutes(1);
        public int SuccessThreshold { get; set; } = 1;
    }
}